package com.zt.bigdata.vertx.interceptor

import java.util.concurrent.ConcurrentHashMap

import com.ecfront.dew.common.Resp
import io.vertx.ext.web.RoutingContext
import org.springframework.stereotype.Component

import scala.concurrent.Future
import scala.reflect.ClassTag

/**
  *
  */
@Component
class AsyncHttpFilterProcessor[E <: Animal, R <: Object] {

  val FILTERS = new ConcurrentHashMap[String, Seq[HttpAsyncFilter[E, R]]]

  def register(group: String, list: HttpAsyncFilter[E, R]*): Unit = {
    FILTERS.put(group, list)
  }

  def process(group: String, e: E, processFunction: ProcessFunction[E, R], routingContext: RoutingContext)(implicit r: ClassTag[R]): Future[Resp[R]] = {
    val chain = new FilterChain(processFunction, FILTERS.getOrDefault(group, Seq.empty[HttpAsyncFilter[E, R]]))
    chain.process(routingContext, e)
  }
}
